/**
 * http://usejsdoc.org/
 * 生成测试数据
 */
let ejs = require('../../ejs/ejsUtil.js');
let path=require('path');
let fs=require('fs');
let mutil=require('../../ejs/EjsDataHandler.js');
let fileName=path.resolve(__dirname,"../../../template/insert.ejs");
let configPath=path.resolve(selfConfig.root,selfConfig.table_conf,"gen_data");

exports.handler=function(data){
	if(selfConfig.test)return ;

	// log(data)
	let sql=`
		insert into \`<%=tableName%>\` (<%=fieldNames%>)
		values (<%-values%>);
	`;
	let result="";

	data.orderTable.forEach((item)=>{//循环表
		var table=data.tables[item];
		var arr=[].concat(table.id,table.field);

		for(var i=0;i<10;i++){//循环写入10条数据
			var fieldNames=arr.map((item)=>{//生成字段名
				return item.attr["@_name"];
			}).join();
			// console.log(fieldNames);
			//生成插入的直
			var values=arr.map((item)=>{

				var typename=item.attr["@_type"];
				try{
					var gen=require(configPath+"/"+typename+".js");
				}catch(e){
					return ""
				}
				return gen.generator(i);
			}).join();
			// console.log(values);
			result+=ejs.render(sql,{
				tableName:item,
				fieldNames:fieldNames,
				values:values
			});

			
		}

		
		
		/*ejs.renderFile(fileName,{
			tableName:item,
			table:table
		},(err,str)=>{
			if(err)console.log(err);
			// fs.writeFileSync(path.resolve(__dirname,selfConfig['base_root'],mutil.toPasic(index)+".java"),str);
		});
		log(arr);*/
	});
	fs.writeFileSync(path.resolve(selfConfig['root'],"insert.sql"),result);

}


